<template>
	<view>
		<view class="u-tips-color u-p-l-20 u-p-t-20 u-p-r-20"><text class="u-font-24">您已加入了以下{{list.length}}个客户组：</text></view>
		<u-cell-group>
			<u-cell-item 
				v-for="(item,index) in list"
				:key="index"
				:title="item.name"
				>
				<text v-if="lastloginenterpriseid == item.id" class="u-type-primary" style="text-decoration: underline;">使用中</text>
				<u-button v-else type="primary" size="mini" @click="exchangeToken(item.id,2)">切换</u-button>
			</u-cell-item>
		</u-cell-group>
		<view class="u-tips-color u-p-l-20 u-p-t-20 u-p-r-20"><text class="u-font-24">个人账户：</text></view>
		<u-cell-group>
			<u-cell-item 
				:title="userdetail.name"
				>
				<u-button v-if="lastloginenterpriseid > 0" type="primary" size="mini" @click="exchangeToken(0,1)">切换</u-button>
				<text v-else class="u-type-primary" style="text-decoration: underline;">使用中</text>
			</u-cell-item>
		</u-cell-group>
		
		<!-- <navigator 
			:url="`details/details?id=${item.id}`" 
			class="list"
			v-for="(item,index) in list"
			:key="index">
			<view class="">
				<u-tag :text="item.type" mode="plain" type="primary" size="mini"/>
				<text class="u-font-32 u-p-l-10">{{item.name}}</text>
			</view>
			<view class="text">
				<view class="u-flex">
					<text class="label u-tips-color u-font-24">所属地区</text>
					<text class="u-flex-1 u-text-right u-font-24">{{item.cityname}}</text>
				</view>
				<view class="u-flex">
					<text class="label u-tips-color u-font-24">注册时间</text>
					<text class="u-flex-1 u-text-right u-font-24">{{$u.timeFormat(item.regdate.replaceAll('-','/'), 'yyyy-mm-dd hh:MM:ss')}}</text>
				</view>
			</view>
		</navigator> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapActions
	} from 'vuex';
	
	export default {
		data() {
			return {
				// list: [],
				pageIndex: 1,
				pageSize: 20
			}
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail,
				header: state => state.Authorization,
				lastloginenterpriseid: state => state.lastloginenterpriseid,
				list: state => state.userdetail.bindenterprises
			})
		},
		onLoad() {
			this.getEnterprisesList()
			console.log('this.userdetail',this.userdetail)
		},
		onReachBottom(e) {
			this.reachBottom()
		},
		methods: {
			getEnterprisesList(){
				// this.$u.post('/Enterprise/List',{
				// 	"pageIndex": this.pageIndex,
				// 	"pageSize": this.pageSize,
				// 	"isCorrelation": true,
				// 	"ParentId": 0
				// }).then((res)=>{
				// 	this.list = res.rows
				// })
			},
			reachBottom() {
				this.pageIndex += 1
				this.getEnterprisesList(false)
			},
			// 切换登录
			exchangeToken(id,type){
				this.$u.vuex('userType', type)
				if(type == 2){
					this.$u.vuex('enterpriseId', id)
					this.$u.vuex('lastloginenterpriseid', id)
				}else{
					this.$u.vuex('lastloginenterpriseid', 0)
				}
				uni.showToast({
					title: '切换成功',
					icon: 'none'
				})
				
				// this.$store.dispatch('getDetails').then((res)=>{
				// 	console.log('切换成功后获取用户详情：',res)
				// })
				// this.$u.get('/User/Account/ExchangeToken?enterpriseId='+id).then((res)=>{
				// 	console.log('切换登录:',res)
				// 	console.log('新token',res.token)
				// 	console.log('旧token',this.header)
				// 	console.log('是否为不同的token',JSON.stringify(res.token) != JSON.stringify(this.header))
				// 	this.$u.vuex('Authorization',res.token)
				// 	this.$u.vuex('exp', res.exp)
				// 	this.$u.vuex('lastloginenterpriseid', res.enterpriseid)
					
				// 	uni.showToast({
				// 		title: '切换成功',
				// 		icon: 'none'
				// 	})
					
				// 	this.$store.dispatch('getDetails').then((res)=>{
				// 		console.log('切换成功后获取用户详情：',res)
				// 	})
				// })
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;
		.text{
			font-size: 24rpx;
			margin: 20rpx 0;
			line-height: 40rpx;
		}
	}
</style>
